<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/6
 * Time: 15:42
 */

namespace app\portal\controller;


use cmf\controller\HomeBaseController;
use Qcloud\Sms\SmsSingleSender;
use think\Cache;
use think\Db;

class ApiController extends HomeBaseController
{

    /**
     * 用户登录
     */
    public function login()
    {

        $post = request()->param();
        $user = Db::name('customer')->where('phone',$post['phone'])->find();
        //判断用户是否已经存在
        if(empty($user))
        {
            Db::name('customer')->insert(['phone'=>$post['phone']]);
            $userId = Db::name('customer')->getLastInsID();
            $user = Db::name('customer')->where('id',$userId)->find();
        }

        return json($user);
    }


    /**
     * @throws \think\Exception
     * 前台首页法律知识接口
     */
    public function law_list()
    {

//        $page = request()->param('page'); //获取页数
        //        $number = 1;                             //每页显示的条数
//        $total = Db::name('law')->where('status',1)->count('id');
//        $total_page = $total/$number;
//        if (isset($page)) { $page  =$page; } else { $page=1; };
//        $start_from = ($page-1) * $number;
//        $list = Db::query("SELECT * FROM cmf_law where position('1,2' in laws) AND status=1");
        $list = Db::name('law')->select()->toArray();
        $post = request()->param('label/a');
//        $post = ['1'];
        foreach ($list as $k=>$v){
            //切割字符串获取对应的文字
            $array = explode(',',$v['laws']);
            $names_array = Db::name('label')->where('id','in',$array)->column('label_name','id');
            $list[$k]['laws'] = implode(",", $names_array);
            //取交集判断
            if(count(array_intersect($post, $array)) !== count($post)){
                unset($list[$k]);
            }
        }
//        foreach ($list as $k=>$v){
//            $array = explode(',',$v['laws']);
//
//        }
        return json($list);
    }

    /**
     * @return \think\response\Json
     * 法律知识详情
     */
    public function law_details()
    {
        $id = request()->param('id');
        $info = Db::name('law')->where('id',$id)->find();
        //切割字符串获取对应的文字
        $array = explode(',',$info['laws']);
        $names_array = Db::name('label')->where('id','in',$array)->column('label_name','id');
        $info['laws'] = implode(",", $names_array);

        return json($info);
    }

    /**
     * @return \think\response\Json
     * 法律知识标签接口
     */
    public function label_list()
    {
        $label = Db::name('label')->select();

        return json($label);
    }


    /**
     * @param string $phone
     * 发送短信接口
     */
    public function sendSms(){
        require_once CMF_ROOT."/src/index.php";
        $appid = config('sms.appid');
        $appkey = config('sms.appkey');
        $phone = request()->param('phone');
        $code = rand(100000,900000);
        //发送短信
        $phoneNumbers = $phone;
        $templateId = 418591;
        $smsSign = "同人法律传媒";
        $params = [$code];
        $ssender = new SmsSingleSender($appid, $appkey);
        $result = $ssender->sendWithParam("86", $phoneNumbers, $templateId,$params, $smsSign, "", "");
        if(strpos($result,'"result":0') !==false){
            return json($code);
        }else{
            return json(0);
        }
    }

}